<?php
namespace app\api\service\provider;
/**
 * UserProvider
 * @Author Vee7
 * @Date 2021/12/23
 * @Time 15:01
 * @Description Vee7
 */

use app\api\model\UserModel;
use app\api\service\serv\ApiLogService;
use app\api\model\ApiLogModel;


class ApiLogProvider implements ApiLogService
{
    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取日志分页列表
     */
    public function getApiLogListPage($page,$limit,$orderby='id',$order='asc'):array
    {
        return ApiLogModel::limit((($page-1)*$limit),$limit)->order($orderby, $order)->select()->toArray();
    }

    /***
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取日志总数据量
     */
    public function getApiLogListCount():int
    {
        return ApiLogModel::count();
    }

    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取日志列表
     */
    public function getApiLogList($orderby='id',$order='asc'):array
    {
        return ApiLogModel::order($orderby, $order)->select()->toArray();
    }

    /***
     * @param array $id
     * @return bool
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 9:23
     * @Description 按ID删除日志
     */
    public function delApiLogList(array $id):bool
    {
        $list = ApiLogModel::select($id);
        $result = $list->delete();
        return $result;
    }



    /***
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 14:48
     * @Description 新增日志
     */
    public function addApiLogList($user_id,$user_username,$input,$path,$method,$ip)
    {
        $user = UserModel::where('id',$user_id)->find();
        if($user) {
            $user_id = $user['id'];
            $user_username = $user['username'];
        }else{
            return false;
        }
        $log = new ApiLogModel;
        $log->user_id = $user_id;
        $log->user_name = $user_username;
        $log->method = $method;
        $log->ip_address = $ip;
        $log->content = " 路径：".$path." 参数：".json_encode($input, JSON_UNESCAPED_UNICODE);
        $log->created_time = time();
        return $log->save();
    }

    /***
     * @param $by
     * @Author Vee7
     * @Date 2021/12/22
     * @Time 17:18
     * @Description 获取接口的/年/月/周/日/时数据
     */
    public function getApiLogListCountWhere($by,$time='-1 hours')
    {
        switch ($by)
        {
            case 'year':
                return ApiLogModel::whereYear('create_time')->select()->toArray();
                break;
            case 'month':
                return ApiLogModel::whereMonth('create_time')->select()->toArray();
                break;
            case 'day':
                return ApiLogModel::whereDay('create_time')->select()->toArray();
                break;
            case 'week':
                return ApiLogModel::whereWeek('create_time')->field('create_time')->order('create_time','asc')->select()->toArray();
                break;
            case 'time':
                return ApiLogModel::whereTime('create_time',$time)->select()->toArray();
                break;
            //默认查询近一小时
            default:
                return ApiLogModel::whereTime('create_time','-1 hours')->select()->toArray();
        }
    }
}